package gold.digger;

import gold.utils.InputUtil;

import java.util.*;

/**
 * Created by fanzhenyu02 on 2020/6/27.
 * common problem solver template.
 */
public class LC149 {
    public long startExecuteTime = System.currentTimeMillis();

    /*
        * @param 此题目参考了别人代码
        * 这是因为问题情况较为复杂
        * 未来需要再次复习此道题目
        * @return:
        */
    class Solution {
        public int maxPoints(int[][] points) {
            if (points.length < 3) return points.length;

            int res = 0;
            // travel each point
            for (int i = 0; i < points.length; i++) {
                int curDuplicate = 0;
                int max = 0;//save current point line, the most same lint points
                Map<String, Integer> map = new HashMap<>();
                for (int j = i + 1; j < points.length; j++) {
                    int x = points[j][0] - points[i][0], y = points[j][1] - points[i][1];
                    if (x == 0 && y == 0) {
                        ++curDuplicate;
                        continue;
                    }

                    // go gcd
                    int gcd = gcd(x, y);
                    x /= gcd;
                    y /= gcd;
                    String key = x + "_" + y;
                    map.put(key, map.getOrDefault(key, 0) + 1);
                    max = Math.max(max, map.get(key));
                }

                res = Math.max(res, max + curDuplicate + 1);
            }

            return res;
        }

        public int gcd(int x, int y) {
            while (y != 0) {
                int temp = x % y;
                x = y;
                y = temp;
            }
            return x;
        }
    }

    public void run() {
        Solution solution = new Solution();
        int[][] arr = InputUtil.toDoubleIntegerArray("[[1,1],[3,2],[5,3],[4,1],[2,3],[1,4]]");
        System.out.println(solution.maxPoints(arr));
    }

    public static void main(String[] args) throws Exception {
        LC149 an = new LC149();
        an.run();

        System.out.println("\ncurrent solution total execute time: " + (System.currentTimeMillis() - an.startExecuteTime) + " ms.");
    }
}
